/*
 * Copyright (C) 2011 PickleMan <admin.aegamesi@gmail.com>
 * Copyright (C) 2011 IsmAvatar <IsmAvatar@gmail.com>
 * 
 * This file is part of Jeie.
 * Jeie is free software and comes with ABSOLUTELY NO WARRANTY.
 * See LICENSE for details.
 */

package org.jeie.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout.Alignment;

import org.jeie.actions.HorizontalFlipAction;
import org.jeie.actions.NewImageAction;
import org.jeie.actions.ResizeAction;
import org.jeie.actions.RotateAction;
import org.jeie.actions.ScaleAction;
import org.jeie.actions.VerticalFlipAction;
import org.jeie.gui.panels.MainPanel;
import org.jeie.main.EasyImageEditor;
import org.jeie.messages.Messages;

public class Menu implements ActionListener {
	public JMenuItem item_new;
	public JMenuItem item_open;
	public JMenuItem item_save;
	public JMenuItem item_exit;

	public JMenuItem item_undo;
	public JMenuItem item_redo;
	public JMenuItem item_cut;
	public JMenuItem item_copy;
	public JMenuItem item_paste;
	public JMenuItem item_select_all;

	public JMenuItem item_scale;
	public JMenuItem item_resize;
	public JMenuItem item_flip_hor;
	public JMenuItem item_flip_vert;
	public JMenuItem item_rot_90;
	public JMenuItem item_rot_180;
	public JMenuItem item_rot_270;

	public JMenuItem item_help;
	public JMenuItem item_about;

	public MainPanel mp;

	public Menu(MainPanel mp) {
		this.mp = mp;
		JMenuBar menubar = new JMenuBar();
		EasyImageEditor.frame.setJMenuBar(menubar);
		JMenu fileMenu = new JMenu(Messages.getString("Menu.file_menu")); //$NON-NLS-1$
		JMenu editMenu = new JMenu(Messages.getString("Menu.edit_menu")); //$NON-NLS-1$
		JMenu imageMenu = new JMenu(Messages.getString("Menu.image_menu")); //$NON-NLS-1$
		JMenu helpMenu = new JMenu(Messages.getString("Menu.help_menu")); //$NON-NLS-1$
		menubar.add(fileMenu);
		menubar.add(editMenu);
		menubar.add(imageMenu);
		menubar.add(helpMenu);

		item_new = makeItem("new"); //$NON-NLS-1$
		item_open = makeItem("open"); //$NON-NLS-1$
		item_save = makeItem("save"); //$NON-NLS-1$
		item_exit = makeItem("exit"); //$NON-NLS-1$

		item_undo = makeItem("undo"); //$NON-NLS-1$
		item_redo = makeItem("redo"); //$NON-NLS-1$
		item_cut = makeItem("cut"); //$NON-NLS-1$
		item_copy = makeItem("copy"); //$NON-NLS-1$
		item_paste = makeItem("paste"); //$NON-NLS-1$
		item_select_all = makeItem("select_all"); //$NON-NLS-1$

		item_scale = makeItem("scale"); //$NON-NLS-1$
		item_resize = makeItem("resize"); //$NON-NLS-1$
		item_flip_hor = makeItem("flip_hor"); //$NON-NLS-1$
		item_flip_vert = makeItem("flip_vert"); //$NON-NLS-1$
		item_rot_90 = makeItem("rot_90"); //$NON-NLS-1$
		item_rot_180 = makeItem("rot_180"); //$NON-NLS-1$
		item_rot_270 = makeItem("rot_270"); //$NON-NLS-1$

		item_help = makeItem("help"); //$NON-NLS-1$
		item_about = makeItem("about"); //$NON-NLS-1$

		fileMenu.add(item_new);
		fileMenu.add(item_open);
		fileMenu.add(item_save);
		fileMenu.add(item_exit);

		editMenu.add(item_undo);
		editMenu.add(item_redo);
		editMenu.addSeparator();
		editMenu.add(item_cut);
		editMenu.add(item_copy);
		editMenu.add(item_paste);
		editMenu.addSeparator();
		editMenu.add(item_select_all);

		imageMenu.add(item_scale);
		imageMenu.add(item_resize);
		imageMenu.addSeparator();
		imageMenu.add(item_flip_hor);
		imageMenu.add(item_flip_vert);
		imageMenu.addSeparator();
		imageMenu.add(item_rot_90);
		imageMenu.add(item_rot_180);
		imageMenu.add(item_rot_270);

		helpMenu.add(item_help);
		helpMenu.addSeparator();
		helpMenu.add(item_about);

		item_new.addActionListener(this);
		item_open.addActionListener(this);
		item_save.addActionListener(this);
		item_exit.addActionListener(this);

		item_undo.addActionListener(this);
		item_redo.addActionListener(this);
		item_cut.addActionListener(this);
		item_copy.addActionListener(this);
		item_paste.addActionListener(this);
		item_select_all.addActionListener(this);

		item_scale.addActionListener(this);
		item_resize.addActionListener(this);
		item_flip_hor.addActionListener(this);
		item_flip_vert.addActionListener(this);
		item_rot_90.addActionListener(this);
		item_rot_180.addActionListener(this);
		item_rot_270.addActionListener(this);

		item_help.addActionListener(this);
		item_about.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		// FILE
		if (e.getSource() == item_new) {
			Dimension d = showDimensionsDialog(EasyImageEditor.frame);
			if (d != null) {
				mp.gui.canvas.doAction(new NewImageAction(d, mp.toolbox.cp
						.getBackground()));
				mp.gui.repaint();
			}
		}
		if (e.getSource() == item_open) {

		}
		if (e.getSource() == item_save) {
			JFileChooser fc = new JFileChooser();
			int returnVal = fc.showSaveDialog(EasyImageEditor.frame);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				try {
					// TODO allow other formats
					ImageIO.write(mp.gui.canvas.saveRender(false), "png", file); //$NON-NLS-1$
				} catch (IOException exception) {
					exception.printStackTrace();
				}
			}
		}
		if (e.getSource() == item_exit)
			EasyImageEditor.frame.dispose();

		// EDIT

		// IMAGE
		if (e.getSource() == item_resize) {
			Dimension d = showDimensionsDialog(EasyImageEditor.frame);
			if (d != null) {
				mp.gui.canvas.doAction(new ResizeAction(d, mp.toolbox.cp
						.getBackground()));
				mp.gui.repaint();
			}
		}
		if (e.getSource() == item_scale) {
			Dimension d = showDimensionsDialog(EasyImageEditor.frame);
			if (d != null) {
				mp.gui.canvas.doAction(new ScaleAction(d, mp.toolbox.cp
						.getBackground()));
				mp.gui.repaint();
			}
		}
		if (e.getSource() == item_flip_hor) {
			EasyImageEditor.mainPanel.gui.canvas
					.doAction(new HorizontalFlipAction());
			EasyImageEditor.mainPanel.gui.repaint();
		}
		if (e.getSource() == item_flip_vert) {
			EasyImageEditor.mainPanel.gui.canvas
					.doAction(new VerticalFlipAction());
			EasyImageEditor.mainPanel.gui.repaint();
		}
		if (e.getSource() == item_rot_90) {
			EasyImageEditor.mainPanel.gui.canvas.doAction(new RotateAction(90));
			EasyImageEditor.mainPanel.gui.repaint();
		}
		if (e.getSource() == item_rot_180) {
			EasyImageEditor.mainPanel.gui.canvas
					.doAction(new RotateAction(180));
			EasyImageEditor.mainPanel.gui.repaint();
		}
		if (e.getSource() == item_rot_270) {
			EasyImageEditor.mainPanel.gui.canvas
					.doAction(new RotateAction(270));
			EasyImageEditor.mainPanel.gui.repaint();
		}
	}

	private Dimension showDimensionsDialog(Component parent) {
		JPanel p = new JPanel();
		GroupLayout layout = new GroupLayout(p);
		layout.setAutoCreateGaps(false);
		layout.setAutoCreateContainerGaps(false);
		p.setLayout(layout);

		JLabel caption = new JLabel(Messages.getString("Menu.NEW_CAPTION")); //$NON-NLS-1$
		JLabel labw = new JLabel(Messages.getString("Menu.NEW_WIDTH")); //$NON-NLS-1$
		JLabel labh = new JLabel(Messages.getString("Menu.NEW_HEIGHT")); //$NON-NLS-1$
		NumberField numw = new NumberField(1, Integer.MAX_VALUE, 0);
		NumberField numh = new NumberField(1, Integer.MAX_VALUE, 0);

		layout.setHorizontalGroup(layout.createParallelGroup()
		/**/.addComponent(caption, Alignment.CENTER)
		/**/.addGroup(layout.createSequentialGroup()
		/*	*/.addGroup(layout.createParallelGroup()
		/*		*/.addComponent(labw)
		/*		*/.addComponent(labh))
		/*	*/.addGap(5)
		/*	*/.addGroup(layout.createParallelGroup()
		/*		*/.addComponent(numw)
		/*		*/.addComponent(numh))));

		layout.setVerticalGroup(layout.createSequentialGroup()
		/*	*/.addComponent(caption)
		/*	*/.addGap(5)
		/*	*/.addGroup(layout.createParallelGroup()
		/*		*/.addComponent(labw)
		/*		*/.addComponent(numw))
		/*	*/.addGroup(layout.createParallelGroup()
		/*		*/.addComponent(labh)
		/*		*/.addComponent(numh)));

		if (JOptionPane.showConfirmDialog(parent, p,
				Messages.getString("Menu.NEW_TITLE"), //$NON-NLS-1$
				JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION)
			return null;

		return new Dimension(numw.getIntValue(), numh.getIntValue());
	}

	private JMenuItem makeItem(String s) {
		return new JMenuItem(Messages.getString("Menu." + s + "_item")); //$NON-NLS-1$ //$NON-NLS-2$
	}
}